package struts;

import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ActionServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException{
		  System.out.println("调用doGet方法");
		  response.setContentType("text;html;charset=utf-8");
		  //得到请求头
		  String path = this.getPtah(request.getServletPath());
		  //取得XML
		  Map<String,ConfigBean> map = (Map<String,ConfigBean>)this.getServletContext().getAttribute("struts-cofig");
		  System.out.println(path);
		  ConfigBean bean = map.get(path);
		  ActionForm form = null;
		  Action action = null;
			try {
				form = (ActionForm)FullBean.getBean(bean.getFormClass(),request);
				Class c = Class.forName(bean.getActionClass());
				action = (Action)c.newInstance();
				String url = action.excuse(form);
				//转发
				url = bean.getForwardList().get(url);
				System.out.println(url);
				RequestDispatcher dis = request.getRequestDispatcher(url);
				dis.forward(request, response);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		  System.out.println("servlet test success....");
		 }

		protected void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException{
		 System.out.println("调用doPost方法");
		 doGet(request,response);
		}
		
		private String getPtah(String servletpath){
			return servletpath.split("\\.")[0];
		}
}
